<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/**
 * 
 * 用户管理 ...
 * @author alang
 *
 */

class Users extends Admin_controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_mdl','user');
	}
	
	/**
	 * 
	 * 
	 */
	public function index()
	{
		$user = array();
		
		
		/*分页设置*/
		$page = $this->uri->rsegment(4);
		$page = ($page && is_numeric($page)) ? intval($page) : 1;
		$limit = 1000;
		$offset = ($page - 1) * $limit;
		$pagination = '';
		
		$count = $this->user->get_user_count();
		$this->load->library('pagination');
		$config['base_url'] = base_url().'/adminroot/users/index/page';
		$config['total_rows'] = $count;
		//设置url上第几段用于传递分页器的偏移量
		$config ['uri_segment'] = 5;	 
		$config['per_page'] = $limit; 
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		$data['page'] = $this->pagination->create_links();
			
		$user = $this->user->get_all_user(true,$limit, $offset,'DESC');
		$data['list'] = $user;

		$this->load->view('adminroot/user_list',$data);
		
	}
	
	/**
	 * 
	 * 添加用户 ...
	 */
	public function add_user()
	{
		$this->_load_validation_rules('add');		
		if($this->form_validation->run() == FALSE)
		{
			$this->load->model('grade_mdl','grade');
			$grade = $this->grade->get_all_grade();
			
			$this->load->model('country_mdl','country');
			$country = $this->country->get_all_country();
			
			$data['grade'] = $grade;
			$data['country'] = $country;
			$this->load->view('adminroot/add_user',$data);
		}
		else
		{
			$result = $this->user_mdl->add_user(
				array(
					'txusername' => $this->input->post('txusername'),
					'txpassword' => Common::do_hash($this->input->post('txpassword')),
					'txemail' => $this->input->post('txemail'),
					'headpic' => $this->input->post('headerpic'),
					'txgrade' => $this->input->post('txgrade'),
					'txgoldnum' => $this->input->post('txgoldnum'),
					'txcountry' => $this->input->post('txcountry'),
					'txaddress' => $this->input->post('txaddress'),
					'txaccount' => $this->input->post('txaccount'),
					'enabled' => $this->input->post('enabled')
				)
			);
			
			if($result)
			{
				redirect('adminroot/users');
			}
		}
	}
	
	/**
	 * 编辑
	 */
	
	public function edit_user()
	{	
		$id = intval($this->uri->rsegment(4));
		if(empty($id))
		{
			$id = $this->input->post('id');
		}
		$data['user'] = $this->user->get_user_by_id($id);
		
		$this->_load_validation_rules('edit');
		if($this->form_validation->run() == FALSE)
		{
			
			$this->load->model('grade_mdl','grade');
			$grade = $this->grade->get_all_grade();
			
			$this->load->model('country_mdl','country');
			$country = $this->country->get_all_country();
			
			$data['grade'] = $grade;
			$data['country'] = $country;
			$this->load->view('adminroot/edit_user',$data);
		}
		else 
		{
		
				$result = $this->user_mdl->update_user(
					intval($this->input->post('id')),
					array(
						'txusername' => $this->input->post('txusername'),
						'txemail' =>  $this->input->post('txemail'),
						'headpic' => $this->input->post('headpic'),
						'txgrade' => $this->input->post('txgrade'),
						'txgoldnum' => $this->input->post('txgoldnum'),
						'txcountry' => $this->input->post('txcountry'),
						'txaddress' => $this->input->post('txaddress'),
						'txaccount' => $this->input->post('txaccount'),
						'enabled' => $this->input->post('enabled')
					)
			);
			if($result)
			{
				redirect('adminroot/users');
			}
		}
	}
	
	/**
	 *  删除用户
	 */
	
	public function del_user()
	{
		$id = $this->uri->rsegment(4);
		$result = $this->user_mdl->del_user($id);
		return $result;
	}
	/**
	 * 
	 * 清除账户(test@163.com除外)
	 */
	
	public function del_all_user()
	{
		$this->db->where('txemail !=','test@163.com');
		$this->db->delete('cj_users');
	}
	
	 /**
     * 配置表单验证规则
     * 
     * @access private
     * @return void
     */
	private function _load_validation_rules($type='add')
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<li>', '</li>');
				
		$this->form_validation->set_rules('txusername','姓名','required|xss_clean');
		if($type=='add')
		{
			$this->form_validation->set_rules('txpassword','密码','required|xss_clean');
			$this->form_validation->set_rules('repassword','确认密码','required');
		}

		$this->form_validation->set_rules('txemail','邮箱地址','required|valid_email');
		//$this->form_validation->set_rules('headerpic','头像','required');
		$this->form_validation->set_rules('txgrade','等级','required|numeric');
		$this->form_validation->set_rules('txgoldnum','金币数量','required|numeric');
		$this->form_validation->set_rules('txcountry','国家','required');
		$this->form_validation->set_rules('txaddress','收货地址','required|xss_clean');
		$this->form_validation->set_rules('txaccount','收款账号','required|xss_clean');
	}
}